package com.heu.blood.transfusion.controller;

import java.util.Arrays;
import java.util.Map;

import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.security.entity.LoginUser;
import com.heu.blood.transfusion.entity.TransfusionApplicationEntity;
import com.heu.blood.transfusion.service.TransfusionApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.heu.blood.transfusion.entity.TransfusionReviewEntity;
import com.heu.blood.transfusion.service.TransfusionReviewService;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;



/**
 * 
 *
 * @author myq
 * @email 596921409@qq.com
 * @date 2023-07-17 19:33:47
 */
@RestController
@RequestMapping("transfusion/review")
public class TransfusionReviewController {
    @Autowired
    private TransfusionReviewService transfusionReviewService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "输血申请审核", operate = "批量查询输血申请审核信息")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = transfusionReviewService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ControllerLogAnnotation(module = "输血申请审核", operate = "根据id查询输血申请审核信息")
    public R info(@PathVariable("id") Long id){
		TransfusionReviewEntity transfusionReview = transfusionReviewService.getById(id);

        return R.ok().put("transfusionReview", transfusionReview);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ControllerLogAnnotation(module = "输血申请审核", operate = "保存输血申请审核信息")
    public R save(@RequestBody TransfusionReviewEntity transfusionReview){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication !=null&&authentication.isAuthenticated()){
            LoginUser loginUser =(LoginUser)  authentication.getPrincipal();
            System.out.println(loginUser.getUser().getUserId());
            System.out.println(loginUser.getUser().getUserName());
            transfusionReview.setReviewer(loginUser.getUser().getUserName());
        }
		boolean result = transfusionReviewService.save(transfusionReview);
        if(!result){
            return R.ok("提交输血申请审核失败");
        }
        return R.ok("提交输血申请审核成功");

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ControllerLogAnnotation(module = "输血申请审核", operate = "修改输血申请审核信息")
    public R update(@RequestBody TransfusionReviewEntity transfusionReview){
		transfusionReviewService.updateById(transfusionReview);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ControllerLogAnnotation(module = "输血申请审核", operate = "批量删除输血申请审核信息")
    public R delete(@RequestBody Long[] ids){
		transfusionReviewService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
